package util;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ThreadLocalRandom;

public class OrderIdUtils {


    /**
     * 时间格式化
     */
    private final static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

    /**
     * 订单id生成规则 年月日时分秒+发货地址码+收货地址码+三位随机码
     *
     * @return
     */
    public final static String orderIdUtils(String sendCode, String receiveCode) {

        /**
         *  如果字符种类不够，可以自己再添加一些
         */
        String range = "0123456789abcdefghijklmnopqrstuvwxyz";
        ThreadLocalRandom random = ThreadLocalRandom.current();

        StringBuilder result = new StringBuilder();

        result.append(dateTimeFormatter.format(LocalDateTime.now()))
                .append(sendCode.substring(0,4))
                .append(receiveCode.substring(0,4));

        /**
         * 要生成几位，就把这里的数字改成几 目前业务要求三位随机码
         */
        for (int i = 0; i < 3; i++) {
            result.append(range.charAt(random.nextInt(range.length())));
        }

        return result.toString();
    }


    /**
     * 流水号生成
     * @return
     */
    public final static String serialNoUtils(){

        /**
         *  如果字符种类不够，可以自己再添加一些
         */
        String range = "0123456789abcdefghijklmnopqrstuvwxyz";
        ThreadLocalRandom random = ThreadLocalRandom.current();

        StringBuilder result = new StringBuilder();

        result.append(dateTimeFormatter.format(LocalDateTime.now()));

        /**
         * 要生成几位，就把这里的数字改成几 目前业务要求三位随机码
         */
        for (int i = 0; i < 8; i++) {
            result.append(range.charAt(random.nextInt(range.length())));
        }

        return result.toString();
    }

}
